﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using NJ.Dto.RoleMenu;

namespace NJ.Web.Html
{
    public static class HtmlExt
    {
        #region 获取左侧树桩菜单

        /// <summary>
        /// 获取左侧树桩菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="menuDtos"></param>
        /// <returns></returns>
        public static MvcHtmlString GetHomeMenuTree(this HtmlHelper helper, List<GetHomeMenuTreeDto> menuDtos)
        {
            StringBuilder sb = new StringBuilder();
            GetMenuTree(0, menuDtos, sb);
            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 构建菜单
        /// </summary>
        /// <param name="Pid"></param>
        /// <param name="menuDtos"></param>
        private static void GetMenuTree(int Pid, List<GetHomeMenuTreeDto> menuDtos, StringBuilder sb)
        {
            List<GetHomeMenuTreeDto> menus = menuDtos.Where(x => x.PId == Pid).ToList();
            for (int i = 0; i < menus.Count(); i++)
            {
                var menu = menus[i];
                //如果是顶级导航
                if (Pid == 0)
                {
                    sb.Append("<li>");
                    sb.Append(menu.Exist ? "<a href='javascript:;;'>" : "<a _href='" + menu.Url + " '>");
                    sb.Append("<i class='layui-icon'>" + menu.Icon + "</i>");
                    sb.Append("<cite>" + menu.Title + "</cite>");
                    sb.Append(menu.Exist ? "<i class='layui-icon nav_right'>&#xe603;</i>" : "");
                    sb.Append("</a>");
                    GetMenuTree(menu.MenuId, menuDtos, sb);
                    sb.Append("</li>");

                }
                else //下级导航
                {
                    if (i == 0)
                    {
                        sb.Append("<ul class='sub-menu'>");
                    }
                    sb.Append("<li>");
                    sb.Append(menu.Exist ? "<a href='javascript:;;'>" : "<a _href='" + menu.Url + " '>");
                    sb.Append("<i class='layui-icon'>" + menu.Icon + "</i>");
                    sb.Append("<cite>" + menu.Title + "</cite>");
                    sb.Append(menu.Exist ? "<i class='layui-icon nav_right'>&#xe603;</i>" : "");
                    sb.Append("</a>");
                    GetMenuTree(menu.MenuId, menuDtos, sb);
                    sb.Append("</li>");
                    if (i == (menus.Count() - 1))
                    {
                        sb.Append("</ul>");
                    }
                }
            }
        }
        #endregion
    }
}